package com.ruoyi.web.controller.fs;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.fs.domain.FsCase;
import com.ruoyi.fs.domain.FsCaseProgressCourt;
import com.ruoyi.fs.domain.FsCaseProgressFile;
import com.ruoyi.fs.service.IFsCaseProgressCourtService;
import com.ruoyi.fs.service.IFsCaseProgressFileService;
import com.ruoyi.fs.service.IFsCaseService;
import com.ruoyi.web.controller.tool.OSSUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.domain.FsCaseProgress;
import com.ruoyi.fs.service.IFsCaseProgressService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 案件进展Controller
 *
 * @author hero
 * @date 2024-11-27
 */
@RestController
@RequestMapping("/fs/progress")
public class FsCaseProgressController extends BaseController {
    @Autowired
    private IFsCaseService fsCaseService;
    @Autowired
    private IFsCaseProgressService fsCaseProgressService;
    @Autowired
    private IFsCaseProgressCourtService fsCaseProgressCourtService;
    @Autowired
    private IFsCaseProgressFileService fsCaseProgressFileService;
    @Autowired
    private OSSUtil ossUtil;

    /**
     * 查询案件进展列表
     */
    @PreAuthorize("@ss.hasPermi('fs:case:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCaseProgress fsCaseProgress) {
        startPage();
        List<FsCaseProgress> list = fsCaseProgressService.selectFsCaseProgressList(fsCaseProgress);
        return getDataTable(list);
    }

    /**
     * 导出案件进展列表
     */
    @PreAuthorize("@ss.hasPermi('fs:case:export')")
    @Log(title = "案件进展", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsCaseProgress fsCaseProgress) {
        List<FsCaseProgress> list = fsCaseProgressService.selectFsCaseProgressList(fsCaseProgress);
        ExcelUtil<FsCaseProgress> util = new ExcelUtil<FsCaseProgress>(FsCaseProgress.class);
        util.exportExcel(response, list, "案件进展数据");
    }

    /**
     * 获取案件进展详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:case:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fsCaseProgressService.selectFsCaseProgressById(id));
    }

    /**
     * 新增案件进展
     */
    @PreAuthorize("@ss.hasPermi('fs:case:add')")
    @Log(title = "案件进展", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCaseProgress fsCaseProgress) {
        return toAjax(fsCaseProgressService.insertFsCaseProgress(fsCaseProgress));
    }

    /**
     * 修改案件进展
     */
    @PreAuthorize("@ss.hasPermi('fs:case:edit')")
    @Log(title = "案件进展", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCaseProgress fsCaseProgress) {
        return toAjax(fsCaseProgressService.updateFsCaseProgress(fsCaseProgress));
    }

    /**
     * 删除案件进展
     */
    @PreAuthorize("@ss.hasPermi('fs:case:remove')")
    @Log(title = "案件进展", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fsCaseProgressService.deleteFsCaseProgressByIds(ids));
    }

    /**
     * 获取案件进展详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:case:query')")
    @GetMapping(value = "/getProcessByCaseAndLx/{caseId}/{jzlx}")
    public AjaxResult getProcessByCaseAndLx(@PathVariable("caseId") Long caseId, @PathVariable("jzlx") Integer jzlx) {
        FsCaseProgress process = new FsCaseProgress();
        process.setCaseId(caseId);
        process.setJzlx(jzlx);
        List<FsCaseProgress> olds = fsCaseProgressService.selectFsCaseProgressList(process);
        if (olds.isEmpty()) {
        } else {
            if (jzlx == 4) {
                process = olds.get(0);
                FsCaseProgressCourt progressCourt = new FsCaseProgressCourt();
                progressCourt.setProcessId(process.getId());
                List<FsCaseProgressCourt> courts = fsCaseProgressCourtService.selectFsCaseProgressCourtList(progressCourt);
                if (!courts.isEmpty()) {
                    progressCourt = courts.get(0);
                    progressCourt.setJzlx(process.getJzlx());
                    return success(progressCourt);
                }
            }
        }
        return success();
    }
    /**
     * 删除案件进展文件
     */
    @PreAuthorize("@ss.hasPermi('fs:case:edit')")
    @Log(title = "案件进展", businessType = BusinessType.DELETE)
    @DeleteMapping("/file/{processId}/{name}")
    public AjaxResult removeFile(
            @PathVariable("name") String name,
            @PathVariable("processId") Long processId) {
        FsCaseProgress progress = fsCaseProgressService.selectFsCaseProgressById(processId);
        FsCase fsCase = fsCaseService.selectFsCaseById(progress.getCaseId());
        String path = "案件进展/";
        if (fsCase.getZfid() == null) {
            path += "0/" + fsCase.getId();
        } else {
            path += fsCase.getZfid() + "/" + fsCase.getId();
        }
        ossUtil.delete(path, name);
        FsCaseProgressFile file = new FsCaseProgressFile();
        file.setProcessId(processId);
        file.setName(name);
        List<FsCaseProgressFile> files = fsCaseProgressFileService.selectFsCaseProgressFileList(file);
        if (!files.isEmpty()) {
            for (int i = 0; i < files.size(); i++) {
                fsCaseProgressFileService.deleteFsCaseProgressFileById(files.get(i).getId());
            }
        }
        return success();
    }
}
